package com.yihuan.controller;

import com.yihuan.entity.UserEntity;
import com.yihuan.global.IDCardUtil;
import com.yihuan.global.MessageConstant;
import com.yihuan.global.vms_variable_global;
import com.yihuan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:梦醒
 * Date: 2022/4/19 17:32
 * Dream:
 * 信言不美，美言不信。
 * 善者不辩，辩者不善。
 * 知者不博，博者不知。
 * Function:
 */
@RestController
public class UserController {
    int pageNumber = 0;
    int count = 0;
    //使用map存储数据
    private Map<String, Object> map = new HashMap<>();
    //数据页数和起始值
    private int num;
    //全局访问类
    private vms_variable_global vms_variable = new vms_variable_global();
    @Autowired
    UserService userService;
    
    /**
     * 管理登录成功
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findUserByUserLoginNameAndUserPassword")
    public Map<String, Object> findUserByUserLoginNameAndUserPassword(@RequestBody UserEntity model, HttpSession session) {
        List<UserEntity> findUserByUserLoginNameAndUserPassword = userService.findUserByUserLoginNameAndUserPassword(model);
        //判断数据是否存在
        if (findUserByUserLoginNameAndUserPassword.size() == 1) {
            //设置登录昵称
            model.setUserNickname(findUserByUserLoginNameAndUserPassword.get(0).getUserNickname());
            //设置登录账户编号
            model.setUserID(findUserByUserLoginNameAndUserPassword.get(0).getUserID());
            vms_variable.variableNameTime(session, model);
            map.put("code", MessageConstant.SUCCESS_CODE);
            map.put("explain ", MessageConstant.USER_JUR_NAME_FALL);
            return map;
        }
        //账户登录成功
        map.put("code", MessageConstant.FALL_CODE);
        map.put("explain ", MessageConstant.USER_JUR_NAME_SUCCESS);
        return map;
    }
    
    /**
     * 分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findUserByUserList")
    public Map<String, Object> findUserByUserList(@RequestBody UserEntity model, HttpSession session) {
        if (model.getJurID() == 3) {
            model.setSellerID(1);
        } else if (model.getJurID() == 4) {
            model.setSellerID(1);
        }
        List<UserEntity> findUserByUserList = userService.findUserByUserList(model);
        if (findUserByUserList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.USER_LIMIT_FALL);
            return map;
        }
        pageNumber = countUserList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("user_List", findUserByUserList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.USER_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 统计分页查询数据
     *
     * @param model
     * @return
     */
    public int countUserList(UserEntity model) {
        count = userService.countUserList(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 模糊分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findUserByUserConcatList")
    public Map<String, Object> findUserByUserConcatList(@RequestBody UserEntity model, HttpSession session) {
        if (model.getJurID() == 3) {
            model.setSellerID(1);
        } else if (model.getJurID() == 4) {
            model.setSellerID(1);
        }
        List<UserEntity> findUserByUserConcatList = userService.findUserByUserConcatList(model);
        if (findUserByUserConcatList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.USER_LIMIT_Concat_FALL);
            return map;
        }
        pageNumber = countUserConcatList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("user_List", findUserByUserConcatList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.USER_LIMIT_Concat_SUCCESS);
        return map;
    }
    
    /**
     * 统计模糊分页查询数据
     *
     * @param model
     * @return
     */
    public int countUserConcatList(UserEntity model) {
        count = userService.countUserConcatList(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 更改用户信息状态
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateUserByUserState")
    public Map<String, Object> updateUserByUserState(@RequestBody UserEntity model, HttpSession session) {
        int updateUserByUserState = userService.updateUserByUserState(model);
        //判断数据是否存在
        if (updateUserByUserState == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.USER_STATE_FALL);
            return map;
        }
        //账户登录成功
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.USER_STATE_SUCCESS);
        return map;
    }
    
    /**
     * 删除用户信息状态
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateUserByUserDeleteState")
    public Map<String, Object> updateUserByUserDeleteState(@RequestBody UserEntity model, HttpSession session) {
        int updateUserByUserState = userService.updateUserByUserDeleteState(model);
        //判断数据是否存在
        if (updateUserByUserState == 1) {
            map.put("code", MessageConstant.SUCCESS_CODE);
            map.put("explain ", MessageConstant.USER_DELETE_SUCCESS);
            return map;
        }
        map.put("code", MessageConstant.FALL_CODE);
        map.put("explain ", MessageConstant.USER_DELETE_FALL);
        return map;
    }
    
    /**
     * 普通用户和回收员登录成功
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findUserByUserLoginNameAndUserPasswordAndSellerID")
    public Map<String, Object> findUserByUserLoginNameAndUserPasswordAndSellerID(@RequestBody UserEntity model, HttpSession session) {
        List<UserEntity> findUserByUserLoginNameAndUserPassword = userService.findUserByUserLoginNameAndUserPasswordAndSellerID(model);
        //判断数据是否存在
        if (findUserByUserLoginNameAndUserPassword.size() == 1) {
            //设置登录昵称
            model.setUserNickname(findUserByUserLoginNameAndUserPassword.get(0).getUserNickname());
            //设置登录账户编号
            model.setUserID(findUserByUserLoginNameAndUserPassword.get(0).getUserID());
            if (model.getJurID() == 3) {
                vms_variable.variableNameTime_3(session, model);
                map.put("code", MessageConstant.HALF_CODE);
                map.put("explain ", MessageConstant.USER_LOGIN_HALF_FALL);
                return map;
            } else {
                vms_variable.variableNameTime_4(session, model);
                map.put("code", MessageConstant.SUCCESS_CODE);
                map.put("explain ", MessageConstant.USER_LOGIN_FALL);
                return map;
            }
        }
        //账户登录成功
        map.put("code", MessageConstant.FALL_CODE);
        map.put("explain ", MessageConstant.USER_LOGIN_SUCCESS);
        return map;
    }
    
    
    /**
     * 根据账号判断是否存在
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findUserByUserLoginNameData")
    public Map<String, Object> findUserByUserLoginNameData(@RequestBody UserEntity model, HttpSession session) {
        List<UserEntity> findUserByUserList = userService.findUserByUserLoginNameData(model);
        if (findUserByUserList.size() == 1) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.USER_NAME_SELECT_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("user_List", findUserByUserList);
        map.put("explain ", MessageConstant.USER_NAME_SELECT_SUCCESS);
        return map;
    }
    
    /**
     * 登记用户数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/insertUserByUserData")
    public Map<String, Object> insertUserByUserData(@RequestBody UserEntity model, HttpSession session) {
        if (model.getJurID() == 3) {
            model.setSellerID(1);
            model.setUserState(2);
        } else if (model.getJurID() == 4) {
            model.setSellerID(1);
            model.setUserState(1);
        }
        model.setUserDeleteState(1);
        //设置时间的格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取时间的方法
        Date d = new Date();
        //将获取的时间转换成设置的时间格式进行存储
        model.setUserDate(sf.format(d));
        //用户出生日期
        model.setUserDateOfBirth(new IDCardUtil().getBirthday(model.getUserIDNumber()));
        //用户年龄
        model.setUserAge(new IDCardUtil().getAge(model.getUserIDNumber()));
        
        int insertUserByUserData = userService.insertUserByUserData(model);
        if (insertUserByUserData == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.USER_LIMIT_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.USER_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 根据账号判断是否存在
     *
     * @param session
     * @return
     */
    @RequestMapping("/findUserByUserID")
    public Map<String, Object> findUserByUserID(HttpSession session) {
        UserEntity model = new UserEntity();
        model.setUserID((Integer) session.getAttribute("UserID_4"));
        List<UserEntity> findUserByUserID = userService.findUserByUserID(model);
        if (findUserByUserID.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.USER_NAME_SELECT_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("user_List", findUserByUserID);
        map.put("explain ", MessageConstant.USER_NAME_SELECT_SUCCESS);
        return map;
    }
    
    /**
     * 根据账号判断是否存在
     *
     * @param session
     * @return
     */
    @RequestMapping("/findUserByUserID_1")
    public Map<String, Object> findUserByUserID_1(HttpSession session) {
        UserEntity model = new UserEntity();
        model.setUserID((Integer) session.getAttribute("UserID_3"));
        List<UserEntity> findUserByUserID = userService.findUserByUserID(model);
        if (findUserByUserID.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.USER_NAME_SELECT_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("user_List", findUserByUserID);
        map.put("explain ", MessageConstant.USER_NAME_SELECT_SUCCESS);
        return map;
    }
    
    /**
     * 根据账号判断是否存在
     *
     * @param session
     * @return
     */
    @RequestMapping("/findUserByUserIDData")
    public Map<String, Object> findUserByUserIDData(HttpSession session) {
        UserEntity model = new UserEntity();
        model.setUserID((Integer) session.getAttribute("UserID_3"));
        List<UserEntity> findUserByUserIDData = userService.findUserByUserIDData(model);
        if (findUserByUserIDData.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.USER_NAME_SELECT_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("user_List", findUserByUserIDData);
        map.put("explain ", MessageConstant.USER_NAME_SELECT_SUCCESS);
        return map;
    }
    
    
    /**
     * 统计数据
     *
     * @param session
     * @return
     */
    @RequestMapping("/countUser")
    public Map<String, Object> countUser(HttpSession session) {
        count = userService.countUser();
        map.put("countUser", count);
        return map;
    }
    
}
